package com.csdj.wenjun.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.csdj.wenjun.common.CacheConstant;
import com.csdj.wenjun.common.StatusContant;
import com.csdj.wenjun.common.WebResult;
import com.csdj.wenjun.entity.*;
import com.csdj.wenjun.service.HuojiangjiluService;
import com.csdj.wenjun.service.PtService;
import com.csdj.wenjun.service.SuipianjiluService;
import com.csdj.wenjun.service.UserService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wenjun
 * @since 2022-01-11
 */
@RestController
@RequestMapping("/pt")
public class PtController {
    @Autowired
    private PtService ptService;
    @Autowired
    private SuipianjiluService suipianjiluService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;
    @Autowired
    private HuojiangjiluService huojiangjiluService;
    @Autowired
    private RedissonClient redissonClient;
//    @ResponseBody
//    @GetMapping("/selectptsuipian")
//    public List selectptsuipian(@RequestParam int manheid){
//        QueryWrapper<Ptsuipian> queryWrapper=new QueryWrapper<>();
//        queryWrapper.lambda().eq(Ptsuipian::getManheid,manheid);
//        return ptService.list(queryWrapper);
//    }

    @GetMapping("/selectmanhe")
    public List selectuserid(){
        return ptService.list();
    }
    @GetMapping("/selectptid")
    public Pt selectptid(@RequestParam String id){
        QueryWrapper<Pt> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(Pt::getId,id);
        return ptService.getOne(queryWrapper);
    }
    @GetMapping("/deletesuipianjilu")
    public WebResult deletesuipianjilu(@RequestParam String userid,@RequestParam String ptid){
        QueryWrapper<User> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.lambda().eq(User::getId,userid);
        User user=userService.getOne(queryWrapper1);
        QueryWrapper<Pt> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.lambda().eq(Pt::getId,ptid);
        Pt pt=ptService.getOne(queryWrapper2);
        Huojiangjilu huojiangjilu=new Huojiangjilu();
        huojiangjilu.setHeadurl(user.getHearurl());
        huojiangjilu.setJiangping(pt.getJiangping());
        huojiangjilu.setUserid(user.getId());
        huojiangjilu.setUsername(user.getName());
        huojiangjilu.setPtid(pt.getId());
        huojiangjiluService.save(huojiangjilu);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", ptid);
        updateWrapper.set("yiwangcheng",pt.getYiwangcheng()+1);
        ptService.update(updateWrapper);
        int jg=grabBuy( ptid, 1);
        if(jg!=0){
            try {
                for(int i=0;i<9;i++){
                    QueryWrapper<Suipianjilu> queryWrapper=new QueryWrapper<>();
                    queryWrapper.lambda().eq(Suipianjilu::getPtid,ptid).eq(Suipianjilu::getUserid,userid).eq(Suipianjilu::getSpweizhi,i+1);
                    List<Suipianjilu> list=suipianjiluService.list(queryWrapper);
                    suipianjiluService.removeById(list.get(0).getId());
                }
            }catch (Exception e){
                e.printStackTrace();
                return new WebResult(StatusContant.USER_STATUS_LOCKED);
            }
        }else {
            return new WebResult(StatusContant.USER_STATUS_LOCKED);
        }
        return new WebResult(StatusContant.USER_STATUS_OK,jg);
    }
    @GetMapping("/selecthc")
    public Pt selecthc(@RequestParam int ptid){
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);
        //初始化实际卖出的商品数量0
        Pt pt=(Pt)redisTemplate.opsForValue().get(CacheConstant.PINGTU+ptid);
        if(pt==null){
            QueryWrapper<Pt> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(Pt::getId,ptid);
            return ptService.getOne(queryWrapper);
        }
        return pt;
    }
    // 设置库存
    @GetMapping("/setStockNum")
    public String setStockNum(@RequestParam Integer ptid){

        // 初始化库存数量
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);
        QueryWrapper<Pt> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(Pt::getId,ptid);
        Pt pt=ptService.getOne(queryWrapper);
        redisTemplate.opsForValue().set(CacheConstant.PINGTU+ptid,pt);
        //初始化实际卖出的商品数量0
        return "Ok";
    }
    @GetMapping("/grabBuy")
    public int grabBuy(String ptid,int num) {
        //修改key的序列化类型 提高可读行 StringRedisSerializer设置key的类型为string
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);
        RLock rLock = redissonClient.getLock("product");
        try {
            rLock.lock();
            Pt pt = (Pt)redisTemplate.opsForValue().get(CacheConstant.PINGTU+ptid);
            if (pt.getCount() < 1) {
                return 0;
            }
            // 自减一
            int shuliang=pt.getCount();
            pt.setCount(shuliang-num);
            redisTemplate.opsForValue().set(CacheConstant.PINGTU+ptid,pt);
            System.out.println(pt.getCount());
            return  pt.getCount();
        } finally {
            rLock.unlock();
        }
    }
}
